package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysReceiveOrderConfig;
import com.ruoyi.system.domain.vo.ReceiveOrderCountVo;
import com.ruoyi.system.mapper.RxOrderMapper;
import com.ruoyi.system.mapper.SysReceiveOrderConfigMapper;
import com.ruoyi.system.service.ISysReceiveOrderConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 自动接单配置Controller
 *
 * @author hjf
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/hmk/receive_order_config")
public class SysReceiveOrderConfigController extends BaseController {
    @Autowired
    private ISysReceiveOrderConfigService sysReceiveOrderConfigService;

    @Autowired
    private SysReceiveOrderConfigMapper sysReceiveOrderConfigMapper;

    @Autowired
    private RxOrderMapper rxOrderMapper;

    /**
     * 查询自动接单配置列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:receive_order_config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysReceiveOrderConfig sysReceiveOrderConfig) {
        startPage();
        List<SysReceiveOrderConfig> list = sysReceiveOrderConfigService.selectSysReceiveOrderConfigList(sysReceiveOrderConfig);
        if (list.size() > 0) {
            Map<Long, Integer> as = rxOrderMapper.getHasReceiveOrderByUserIds(list.stream().map(SysReceiveOrderConfig::getUserId).collect(Collectors.toSet())).stream().collect(Collectors.toMap(ReceiveOrderCountVo::getReceiveOrderUserId, ReceiveOrderCountVo::getCount));
            list.stream().forEach(a -> a.setCount(as.get(a.getUserId())));
        }
        return getDataTable(list);
    }


    /**
     * 导出自动接单配置列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:receive_order_config:export')")
    @Log(title = "自动接单配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysReceiveOrderConfig sysReceiveOrderConfig) {
        List<SysReceiveOrderConfig> list = sysReceiveOrderConfigService.selectSysReceiveOrderConfigList(sysReceiveOrderConfig);
        ExcelUtil<SysReceiveOrderConfig> util = new ExcelUtil<SysReceiveOrderConfig>(SysReceiveOrderConfig.class);
        util.exportExcel(response, list, "自动接单配置数据");
    }

    /**
     * 获取自动接单配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('hmk:receive_order_config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysReceiveOrderConfigService.selectSysReceiveOrderConfigById(id));
    }

    /**
     * 新增自动接单配置
     */
    @PreAuthorize("@ss.hasPermi('hmk:receive_order_config:add')")
    @Log(title = "自动接单配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysReceiveOrderConfig sysReceiveOrderConfig) {
        return toAjax(sysReceiveOrderConfigService.insertSysReceiveOrderConfig(sysReceiveOrderConfig));
    }

    /**
     * 修改自动接单配置
     */
    @PreAuthorize("@ss.hasPermi('hmk:receive_order_config:edit')")
    @Log(title = "自动接单配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysReceiveOrderConfig sysReceiveOrderConfig) {
        return toAjax(sysReceiveOrderConfigService.updateSysReceiveOrderConfig(sysReceiveOrderConfig));
    }


    @PermitAll
    @GetMapping(value = "/getInfoByUserId/{userId}")
    public AjaxResult getInfoByUserId(@PathVariable("userId") Long userId) {
        return success(sysReceiveOrderConfigMapper.selectSysReceiveOrderConfigByUserId(userId));
    }

    @PermitAll
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody SysReceiveOrderConfig sysReceiveOrderConfig) {
        SysReceiveOrderConfig sysReceiveOrderConfig2 = sysReceiveOrderConfigMapper.selectSysReceiveOrderConfigByUserId(sysReceiveOrderConfig.getUserId());
        if (null == sysReceiveOrderConfig2) {
            sysReceiveOrderConfig2 = new SysReceiveOrderConfig();
            sysReceiveOrderConfig2.setUserId(sysReceiveOrderConfig.getUserId());
            sysReceiveOrderConfig2.setSwith(sysReceiveOrderConfig.getSwith());
            sysReceiveOrderConfigService.insertSysReceiveOrderConfig(sysReceiveOrderConfig2);
        } else {
            sysReceiveOrderConfig2.setSwith(sysReceiveOrderConfig.getSwith());
            sysReceiveOrderConfigService.updateSysReceiveOrderConfig(sysReceiveOrderConfig2);
        }
        return AjaxResult.success();
    }


    /**
     * 删除自动接单配置
     */
    @PreAuthorize("@ss.hasPermi('hmk:receive_order_config:remove')")
    @Log(title = "自动接单配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysReceiveOrderConfigService.deleteSysReceiveOrderConfigByIds(ids));
    }
}
